<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备编号" prop="deviceno">
        <el-input
          v-model="queryParams.deviceno"
          placeholder="请输入设备编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="IOT设备编号" prop="devicenum">
        <el-input
          v-model="queryParams.devicenum"
          placeholder="请输入IOT设备编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备品牌:对应表bsm_dictionarys" prop="devicebrand">
        <el-input
          v-model="queryParams.devicebrand"
          placeholder="请输入设备品牌:对应表bsm_dictionarys"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务商" prop="serviceprovider">
        <el-input
          v-model="queryParams.serviceprovider"
          placeholder="请输入服务商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="识别等级：1=一级，2=二级，3=三级" prop="identificationlevel">
        <el-input
          v-model="queryParams.identificationlevel"
          placeholder="请输入识别等级：1=一级，2=二级，3=三级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="识别阈值" prop="recognitionthreshold">
        <el-input
          v-model="queryParams.recognitionthreshold"
          placeholder="请输入识别阈值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="对应版本表id" prop="versionid">
        <el-input
          v-model="queryParams.versionid"
          placeholder="请输入对应版本表id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备最新下发时间" prop="lastdowntime">
        <el-date-picker clearable
          v-model="queryParams.lastdowntime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择设备最新下发时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="设备在线状态：1=在线 0=离线 2=故障" prop="isonline">
        <el-input
          v-model="queryParams.isonline"
          placeholder="请输入设备在线状态：1=在线 0=离线 2=故障"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="售出时间" prop="saletime">
        <el-date-picker clearable
          v-model="queryParams.saletime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择售出时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="租户Id" prop="tenantid">
        <el-input
          v-model="queryParams.tenantid"
          placeholder="请输入租户Id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业Id(当前所属项目)" prop="companyid">
        <el-input
          v-model="queryParams.companyid"
          placeholder="请输入企业Id(当前所属项目)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业名称" prop="companyname">
        <el-input
          v-model="queryParams.companyname"
          placeholder="请输入企业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="删除原因" prop="deletereason">
        <el-input
          v-model="queryParams.deletereason"
          placeholder="请输入删除原因"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备密码" prop="devicepassword">
        <el-input
          v-model="queryParams.devicepassword"
          placeholder="请输入设备密码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="sim卡号" prop="simcardno">
        <el-input
          v-model="queryParams.simcardno"
          placeholder="请输入sim卡号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createby">
        <el-input
          v-model="queryParams.createby"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目Id(当前所属项目)" prop="projectid">
        <el-input
          v-model="queryParams.projectid"
          placeholder="请输入项目Id(当前所属项目)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createtime">
        <el-date-picker clearable
          v-model="queryParams.createtime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="修改人" prop="updateby">
        <el-input
          v-model="queryParams.updateby"
          placeholder="请输入修改人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修改时间" prop="updatetime">
        <el-date-picker clearable
          v-model="queryParams.updatetime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择修改时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="删除人" prop="deleteby">
        <el-input
          v-model="queryParams.deleteby"
          placeholder="请输入删除人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="删除时间" prop="deletetime">
        <el-date-picker clearable
          v-model="queryParams.deletetime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择删除时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="项目ID所属表，2为实名制" prop="projectby">
        <el-input
          v-model="queryParams.projectby"
          placeholder="请输入项目ID所属表，2为实名制"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:devices:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:devices:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:devices:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:devices:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="devicesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键Id" align="center" prop="id" />
      <el-table-column label="设备编号" align="center" prop="deviceno" />
      <el-table-column label="IOT设备编号" align="center" prop="devicenum" />
      <el-table-column label="设备品牌:对应表bsm_dictionarys" align="center" prop="devicebrand" />
      <el-table-column label="设备类型：对应表bsm_dictionarys" align="center" prop="devicetype" />
      <el-table-column label="服务商" align="center" prop="serviceprovider" />
      <el-table-column label="识别等级：1=一级，2=二级，3=三级" align="center" prop="identificationlevel" />
      <el-table-column label="识别阈值" align="center" prop="recognitionthreshold" />
      <el-table-column label="对应版本表id" align="center" prop="versionid" />
      <el-table-column label="设备在线状态：1=在线 2=离线(弃用)" align="center" prop="onstatus" />
      <el-table-column label="设备最新下发时间" align="center" prop="lastdowntime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastdowntime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备在线状态：1=在线 0=离线 2=故障" align="center" prop="isonline" />
      <el-table-column label="设备状态： 0-未绑定 1-已绑定 2-已解绑 3-已解绑" align="center" prop="status" />
      <el-table-column label="售出时间" align="center" prop="saletime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.saletime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="租户Id" align="center" prop="tenantid" />
      <el-table-column label="企业Id(当前所属项目)" align="center" prop="companyid" />
      <el-table-column label="企业名称" align="center" prop="companyname" />
      <el-table-column label="删除原因" align="center" prop="deletereason" />
      <el-table-column label="设备密码" align="center" prop="devicepassword" />
      <el-table-column label="是否已报废" align="center" prop="isscrap" />
      <el-table-column label="sim卡号" align="center" prop="simcardno" />
      <el-table-column label="创建人" align="center" prop="createby" />
      <el-table-column label="项目Id(当前所属项目)" align="center" prop="projectid" />
      <el-table-column label="创建时间" align="center" prop="createtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="updateby" />
      <el-table-column label="修改时间" align="center" prop="updatetime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatetime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否删除" align="center" prop="isdeleted" />
      <el-table-column label="删除人" align="center" prop="deleteby" />
      <el-table-column label="删除时间" align="center" prop="deletetime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deletetime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目ID所属表，2为实名制" align="center" prop="projectby" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:devices:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:devices:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备编号" prop="deviceno">
          <el-input v-model="form.deviceno" placeholder="请输入设备编号" />
        </el-form-item>
        <el-form-item label="IOT设备编号" prop="devicenum">
          <el-input v-model="form.devicenum" placeholder="请输入IOT设备编号" />
        </el-form-item>
        <el-form-item label="设备品牌:对应表bsm_dictionarys" prop="devicebrand">
          <el-input v-model="form.devicebrand" placeholder="请输入设备品牌:对应表bsm_dictionarys" />
        </el-form-item>
        <el-form-item label="服务商" prop="serviceprovider">
          <el-input v-model="form.serviceprovider" placeholder="请输入服务商" />
        </el-form-item>
        <el-form-item label="识别等级：1=一级，2=二级，3=三级" prop="identificationlevel">
          <el-input v-model="form.identificationlevel" placeholder="请输入识别等级：1=一级，2=二级，3=三级" />
        </el-form-item>
        <el-form-item label="识别阈值" prop="recognitionthreshold">
          <el-input v-model="form.recognitionthreshold" placeholder="请输入识别阈值" />
        </el-form-item>
        <el-form-item label="对应版本表id" prop="versionid">
          <el-input v-model="form.versionid" placeholder="请输入对应版本表id" />
        </el-form-item>
        <el-form-item label="设备最新下发时间" prop="lastdowntime">
          <el-date-picker clearable
            v-model="form.lastdowntime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择设备最新下发时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="设备在线状态：1=在线 0=离线 2=故障" prop="isonline">
          <el-input v-model="form.isonline" placeholder="请输入设备在线状态：1=在线 0=离线 2=故障" />
        </el-form-item>
        <el-form-item label="售出时间" prop="saletime">
          <el-date-picker clearable
            v-model="form.saletime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择售出时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="租户Id" prop="tenantid">
          <el-input v-model="form.tenantid" placeholder="请输入租户Id" />
        </el-form-item>
        <el-form-item label="企业Id(当前所属项目)" prop="companyid">
          <el-input v-model="form.companyid" placeholder="请输入企业Id(当前所属项目)" />
        </el-form-item>
        <el-form-item label="企业名称" prop="companyname">
          <el-input v-model="form.companyname" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="删除原因" prop="deletereason">
          <el-input v-model="form.deletereason" placeholder="请输入删除原因" />
        </el-form-item>
        <el-form-item label="设备密码" prop="devicepassword">
          <el-input v-model="form.devicepassword" placeholder="请输入设备密码" />
        </el-form-item>
        <el-form-item label="sim卡号" prop="simcardno">
          <el-input v-model="form.simcardno" placeholder="请输入sim卡号" />
        </el-form-item>
        <el-form-item label="项目Id(当前所属项目)" prop="projectid">
          <el-input v-model="form.projectid" placeholder="请输入项目Id(当前所属项目)" />
        </el-form-item>
        <el-form-item label="删除人" prop="deleteby">
          <el-input v-model="form.deleteby" placeholder="请输入删除人" />
        </el-form-item>
        <el-form-item label="删除时间" prop="deletetime">
          <el-date-picker clearable
            v-model="form.deletetime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择删除时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="项目ID所属表，2为实名制" prop="projectby">
          <el-input v-model="form.projectby" placeholder="请输入项目ID所属表，2为实名制" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDevices, getDevices, delDevices, addDevices, updateDevices } from "@/api/system/devices";

export default {
  name: "Devices",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备表格数据
      devicesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceno: null,
        devicenum: null,
        devicebrand: null,
        devicetype: null,
        serviceprovider: null,
        identificationlevel: null,
        recognitionthreshold: null,
        versionid: null,
        onstatus: null,
        lastdowntime: null,
        isonline: null,
        status: null,
        saletime: null,
        tenantid: null,
        companyid: null,
        companyname: null,
        deletereason: null,
        devicepassword: null,
        isscrap: null,
        simcardno: null,
        createby: null,
        projectid: null,
        createtime: null,
        updateby: null,
        updatetime: null,
        isdeleted: null,
        deleteby: null,
        deletetime: null,
        projectby: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deviceno: [
          { required: true, message: "设备编号不能为空", trigger: "blur" }
        ],
        devicebrand: [
          { required: true, message: "设备品牌:对应表bsm_dictionarys不能为空", trigger: "blur" }
        ],
        devicetype: [
          { required: true, message: "设备类型：对应表bsm_dictionarys不能为空", trigger: "change" }
        ],
        status: [
          { required: true, message: "设备状态： 0-未绑定 1-已绑定 2-已解绑 3-已解绑不能为空", trigger: "blur" }
        ],
        tenantid: [
          { required: true, message: "租户Id不能为空", trigger: "blur" }
        ],
        isscrap: [
          { required: true, message: "是否已报废不能为空", trigger: "blur" }
        ],
        createby: [
          { required: true, message: "创建人不能为空", trigger: "blur" }
        ],
        createtime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        isdeleted: [
          { required: true, message: "是否删除不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询设备列表 */
    getList() {
      this.loading = true;
      listDevices(this.queryParams).then(response => {
        this.devicesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deviceno: null,
        devicenum: null,
        devicebrand: null,
        devicetype: null,
        serviceprovider: null,
        identificationlevel: null,
        recognitionthreshold: null,
        versionid: null,
        onstatus: 0,
        lastdowntime: null,
        isonline: null,
        status: 0,
        saletime: null,
        tenantid: null,
        companyid: null,
        companyname: null,
        deletereason: null,
        devicepassword: null,
        isscrap: null,
        simcardno: null,
        createby: null,
        projectid: null,
        createtime: null,
        updateby: null,
        updatetime: null,
        isdeleted: null,
        deleteby: null,
        deletetime: null,
        projectby: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDevices(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDevices(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDevices(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除设备编号为"' + ids + '"的数据项？').then(function() {
        return delDevices(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/devices/export', {
        ...this.queryParams
      }, `devices_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
